<?php
namespace app\wxpro\controller;
use app\BaseController;
use think\facade\Db;
use app\service\personService;
use app\service\deviceService;
use app\service\visitorService;
use think\Log;
class Callback extends BaseController{

    public function yzltest(){
        $path=dir(getcwd())->path.'/callback/yzl/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/140317322929.txt";
        parse_str(file_get_contents($address),$userinfo);
        echo "<pre>";
        var_dump($userinfo);
    }
    public function yzl(){
        $path=dir(getcwd())->path.'/callback/yzl/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        //$address = $path."\\110811525967.txt";
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本
        parse_str(file_get_contents($address),$userinfo);
    }
	public function license(){
		$path=dir(getcwd())->path.'/callback/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        //$address = $path."\\110811525967.txt";
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本
        parse_str(file_get_contents($address),$userinfo);

        //识别回掉
        if(array_key_exists("photoUrl", $userinfo)){
        	$whe["personGuid"]=$userinfo["personGuid"];
	        $personinfo=(new personService())->personinfo($whe);
            //查询设备名称
            $deviceinfo=(new deviceService())->deviceinfo($userinfo["deviceKey"]);
	        //业主家人租客回掉
	        if(!empty($personinfo)){
                $data["guid"]=$userinfo["guid"];
	        	$data["personGuid"]=$userinfo["personGuid"];
	        	$data["deviceKey"]=$userinfo["deviceKey"];
	        	$data["devicename"]=$deviceinfo["devicename"];
	        	$data["access_type"]=$deviceinfo["access_type"];

	        	$data["phone"]=$personinfo["phone"];
	        	$data["name"]=$personinfo["name"];
	        	$data["person_id"]=$personinfo["id"];
	        	$data["relation"]=$personinfo["relation"];
	        	$data["room_id"]=$personinfo["room_id"];
	        	$data["address"]=$personinfo["building"]."幢".$personinfo["unit"]."单元".$personinfo["room"]."室";
                $data["community_id"]=$personinfo["community_id"];
                $data["community_name"]=$personinfo["community_name"];
                $data["group"]=$personinfo["group"];
                $data["building"]=$personinfo["building"];
                $data["unit"]=$personinfo["unit"];
                $data["room"]=$personinfo["room"];
                $data["mzt"]=$personinfo["mzt"];
                
	        	$data["recMode"]=$userinfo["recMode"];
	        	$data["photoUrl"]=$userinfo["photoUrl"];
                //$data["temperature"]=$userinfo["temperature"];
	        	$data["showTime"]=strtotime($userinfo["showTime"]);
                $t=Db::table("record")
                    ->where("guid",$data["guid"])
                    ->find();

                if(empty($t)){
                    $id=Db::table("record")->insertGetId($data);
                    $data["id"]=$id;
                    $data["type"]="1";
                    //$this->create_local_img($data);
                }
	        	
                echo 1;
                return ;
	        }

            //访客回调
            $visitorinfo=(new visitorService())->visitorGuid($userinfo["personGuid"]);
            if(!empty($visitorinfo)){
               
                $data["personGuid"]=$userinfo["personGuid"];
                $data["deviceKey"]=$userinfo["deviceKey"];
                $data["devicename"]=$deviceinfo["devicename"];
                $data["access_type"]=$deviceinfo["access_type"];

                $data["phone"]=$visitorinfo["phone"];
                $data["name"]=$visitorinfo["name"];
                $data["person_id"]=$visitorinfo["id"];
                $data["relation"]=3;
                $data["room_id"]=$visitorinfo["room_id"];
                $data["address"]=$visitorinfo["building"]."幢".$visitorinfo["unit"]."单元".$visitorinfo["room"]."室";
                $data["community_id"]=$personinfo["community_id"];
                $data["community_name"]=$personinfo["community_name"];
                $data["group"]=$personinfo["group"];
                $data["building"]=$personinfo["building"];
                $data["unit"]=$personinfo["unit"];
                $data["room"]=$personinfo["room"];
                $data["mzt"]=$personinfo["mzt"];
                
                $data["recMode"]=$userinfo["recMode"];
                $data["photoUrl"]=$userinfo["photoUrl"];
                $data["showTime"]=strtotime($userinfo["showTime"]);

                /*$t=Db::table("record")
                    ->where("personGuid",$data["personGuid"])
                    ->where("showTime",$data["showTime"])
                    ->where("deviceKey",$data["deviceKey"])
                    ->find();*/
                $t=Db::table("record")
                    ->where("guid",$data["guid"])
                    ->find();
                if(empty($t)){
                    $id=Db::table("record")->insertGetId($data);
                    $data["id"]=$id;
                    $data["type"]="1";
                    //$this->create_local_img($data);
                }
                
                echo 2;
                return ;
            }

            //认证比对
            if(array_key_exists("idCardInfo", $userinfo)){
                if(!empty($userinfo["idCardInfo"])){
                    $data["personGuid"]=$userinfo["personGuid"];
                    $data["deviceKey"]=$userinfo["deviceKey"];
                    $data["devicename"]=$deviceinfo["devicename"];
	        	    $data["access_type"]=$deviceinfo["access_type"];
                    $data["community_id"]=$deviceinfo["community_id"];
                    $data["community_name"]=$deviceinfo["community_name"];
                    
                    $data["relation"]=3;
                   
                    $data["recMode"]=$userinfo["recMode"];
                    $data["photoUrl"]=$userinfo["photoUrl"];
                    $data["idCardInfo"]=$userinfo["idCardInfo"];
                    $data["showTime"]=strtotime($userinfo["showTime"]);
                    $t=Db::table("record")
                        ->where("personGuid",$data["personGuid"])
                        ->where("showTime",$data["showTime"])
                        ->where("deviceKey",$data["deviceKey"])
                        ->find();
                    if(empty($t)){
                        $id=Db::table("record")->insertGetId($data);
                    }    
                    $id=Db::table("record")->insertGetId($data);
                   
                    echo 3;
                    //$this->create_local_img($data);
                    // Log::write($userinfo,"回调");
                    return ;
                }else{
                    $data["personGuid"]=$userinfo["personGuid"];
                    $data["deviceKey"]=$userinfo["deviceKey"];
                    $data["devicename"]=$deviceinfo["devicename"];
                    $data["access_type"]=$deviceinfo["access_type"];
                    $data["community_id"]=$deviceinfo["community_id"];
                    $data["community_name"]=$deviceinfo["community_name"];
                    $data["group"]=$deviceinfo["community_name"];
                    $data["building"]=$deviceinfo["building"];
                    $data["unit"]=$deviceinfo["unit"];
                    
                    $data["relation"]=6;
                    $data["recMode"]=$userinfo["recMode"];
                    $data["photoUrl"]=$userinfo["photoUrl"];
                   
                    $data["showTime"]=strtotime($userinfo["showTime"]);
                    $t=Db::table("record")
                        ->where("personGuid",$data["personGuid"])
                        ->where("showTime",$data["showTime"])
                        ->where("deviceKey",$data["deviceKey"])
                        ->find();
                    if(empty($t)){
                        $id=Db::table("record")->insertGetId($data);
                    }   
                    //$this->create_local_img($data);
                    // Log::write($userinfo,"回调");
                    return ;
                }
                
            }
           
        }
	}
	public function create_local_img($arr){
		$webset=Db::table("webset")->where("id",1)->find();
		$url=$webset["map_url"]."/index/person/index";
        //$url="http://i24o892560.qicp.vip/parkingHzcity/public/index.php/index/Yzl/index";
        //sleep(2);
        //sleep(20);
		$res=$this->yzl_fsockopen($url,$arr);
	}


	public function yzl_fsockopen($url,$post_data=array(),$cookie=array()){
        $url_arr = parse_url($url);
        $port = isset($url_arr['port'])?$url_arr['port']:80;
        if($url_arr['scheme'] == 'https'){
            $url_arr['host'] = 'ssl://'.$url_arr['host'];
        }
        $fp = fsockopen($url_arr['host'],$port,$errno,$errstr,30);
        if(!$fp) return false;
        $getPath = isset($url_arr['path'])?$url_arr['path']:'/index.php';
        $getPath .= isset($url_arr['query'])?'?'.$url_arr['query']:'';
        $method = 'GET';  //默认get方式
        if(!empty($post_data)) $method = 'POST';
        $header = "$method  $getPath  HTTP/1.1\r\n";
        $header .= "Host: ".$url_arr['host']."\r\n";
        if(!empty($cookie)){  //传递cookie信息
            $_cookie = strval(NULL);
            foreach($cookie AS $k=>$v){
                $_cookie .= $k."=".$v.";";
            }
            $cookie_str = "Cookie:".base64_encode($_cookie)."\r\n";
            $header .= $cookie_str;
        }
        if(!empty($post_data)){  //传递post数据
            $_post = array();
            foreach($post_data AS $_k=>$_v){
                $_post[] = $_k."=".urlencode($_v);
            }
            $_post = implode('&', $_post);
            $post_str = "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n";
            $post_str .= "Content-Length: ".strlen($_post)."\r\n";  //数据长度
            $post_str .= "Connection:Close\r\n\r\n";
            $post_str .= $_post;  //传递post数据
            $header .= $post_str;
        }else{
            $header .= "Connection:Close\r\n\r\n";
        }
        fwrite($fp, $header);
        //echo fread($fp,1024);
        usleep(1000); // 这一句也是关键，如果没有这延时，可能在nginx服务器上就无法执行成功
        fclose($fp);
        return true;
    }
}